package org.kyw.basic.util;

import org.kyw.user.domain.Logininfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContext {

    public static Logininfo getCurrentLogin(HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return null;
        }
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        RedisTemplate redisTemplate = (RedisTemplate) webApplicationContext.getBean("redisTemplate");
        Object logininfoTmp = redisTemplate.opsForValue().get(token);
        if(logininfoTmp == null){
            return null;
        }
        return (Logininfo) logininfoTmp;
    }
}
